// RendererTestView.cpp : implementation of the CRendererTestView class
//

#include "stdafx.h"
#include "RendererTest.h"

#include "RendererTestDoc.h"
#include "RendererTestView.h"
#include <oglRenderer/win32RenderDevice.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CRendererTestView

IMPLEMENT_DYNCREATE(CRendererTestView, CView)

BEGIN_MESSAGE_MAP(CRendererTestView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// CRendererTestView construction/destruction

CRendererTestView::CRendererTestView()
{
	// TODO: add construction code here
	m_pCDC = NULL;

}

CRendererTestView::~CRendererTestView()
{
	delete m_pCDC;
}

BOOL CRendererTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CRendererTestView drawing

void CRendererTestView::OnDraw(CDC* /*pDC*/)
{
	CRendererTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	
	CRect rcClient;
	this->GetClientRect(&rcClient);

	ViewPort viewport(0,0,rcClient.Width(),rcClient.Height());
	
	pDoc->GetScene()->GetActiveCamera()->SetAspect((Real)rcClient.Width()/rcClient.Height());

	m_pRenderDevice->GetRenderer(0)->StartRender();
	m_pRenderDevice->GetRenderer(0)->Render(pDoc->GetScene(), viewport );
	m_pRenderDevice->GetRenderer(0)->EndRender();
}


// CRendererTestView printing

BOOL CRendererTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CRendererTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CRendererTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CRendererTestView diagnostics

#ifdef _DEBUG
void CRendererTestView::AssertValid() const
{
	CView::AssertValid();
}

void CRendererTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CRendererTestDoc* CRendererTestView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRendererTestDoc)));
	return (CRendererTestDoc*)m_pDocument;
}
#endif //_DEBUG


// CRendererTestView message handlers

int CRendererTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	m_pCDC = new CClientDC(this);
	m_pRenderDevice = new Win32RenderDevice(m_pCDC->GetSafeHdc());
	
	// TODO:  Add your specialized creation code here
	m_pRenderDevice->CreateGLRenderer(RendererSetting());
	
	return 0;
}

BOOL CRendererTestView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	//not erase background
	return true; 
}
